Skip to content

feat(agents): 提示可以带文件和图片附件,按 ACP 的方式分成图片和文件链接(API-45 0 内补充) - #224

Merged
xiaoyumuxi merged 2 commits into
mainfrom
feat/agent-prompt-images
Nov 28, 2026
Merged

xiaoyumuxi merged 2 commits into
mainfrom
feat/agent-prompt-images

Conversation

@xiaoyumuxi

@xiaoyumuxi xiaoyumuxi commented Nov 28, 2026 •

Copy link
Copy Markdown
Contributor

概述 / Summary

API-45(Agent)的 agents 0 内补充:用户可以在 Agent 对话的提示里插入、粘贴或拖入文件、文件夹和图片,交给 Agent CLI。

做法照 ACP 客户端发提示内容的方式:附件只传路径、不限大小,CLI 像读用户点名的文件那样自己去读。Core 把附件分成两类:

  • 图片:文件开头字节是 Agent 在清单里声明接受的图片类型,相当于 ACP 的 image 内容块;
  • 文件链接:其余文件和文件夹,相当于 ACP 的 resource_link,后端把它写成 [@名称](file:///…) 放进提示正文。
  1. 插件声明 Agent 接受哪些图片:清单 agents[].native 新增可选 imageTypes,取值是 image/png、image/jpeg、image/gif、image/webp 中不重复的若干项。Agent 目录条目同名带出。没声明时所有附件都按文件链接传。
  2. Core 描述附件:agents.prompt 新增可选 attachments: [{path}]。
    • 按用户的顺序,最多 64 个,不限大小;
    • 每个都必须是存在的文件或文件夹的绝对路径;
    • Core 读文件开头 12 字节判断类型,不看扩展名。声明了的类型当作图片;文件夹、空文件、读不了的文件、类型没声明或名不副实的文件都当作文件链接;
    • 带附件时正文可以为空;
    • 被拒绝的提示不开始回合。附件路径不进日志和拒绝信息。
  3. 后端按 CLI 自己的格式传附件:agents/didPrompt 在有附件时带 attachments,没有附件时和以前一样。
    • 图片:{type: "image", path, name, uri, mimeType};
    • 文件链接:{type: "file", path, name, uri};
    • uri 是路径的 file: URI,文件夹以 / 结尾;name 是文件名或文件夹名。

没有新增能力版本、协议版本、宿主方法或宿主通知。旧 Core 以 plugin.manifest.invalid 拒绝声明了 imageTypes 的清单,也拒绝带 attachments 的 agents.prompt。

本 PR 的第一个提交(68b19cf)按“只收图片、最多 8 张、合计 10 MiB”实现;第二个提交(ca60f3e)按评审意见改成上面的附件模型:图片和文件走同一套逻辑,不限大小。两个提交一起看即可,最终行为以本描述为准。

方法与命令 / Methods and commands

没有新增插件宿主方法或宿主通知,只给已有的一条命令和一条通知加了可选字段:

原计划编号 方法 用途
API-45 agents.prompt(平台宿主命令,新增可选 attachments) 把用户的提示连同附件路径交给 Core
API-45 agents/didPrompt(已有通知,新增可选 attachments) 把 Core 分好类的图片和文件链接交给插件后端

主要改动 / Changes

  • 清单:rust/crates/plugin-agents/src/lib.rs 解析并校验 native.imageTypes(只认四种类型、不重复、区分大小写),Agent 目录条目带出 imageTypes。
  • 运行时:rust/crates/agent-runtime/src/live.rs
    • 会话开始时记下 Agent 声明的图片类型;
    • agents.prompt 依次检查正文长度、附件个数、会话、逐个附件(绝对路径、存在),全部通过才开始回合;
    • 每个附件用 url::Url::from_file_path / from_directory_path 算出 uri(新增依赖 url,工作区里已有同版本);
    • 错误码一码一义:
      • agents.invalidAttachment:不是绝对路径或路径不存在;
      • agents.tooManyAttachments:超过 64 个;
    • agents.invalidPrompt 的说明改为“正文超过 1 MiB,或没有附件时正文为空”。
  • 四种官方 SDK:没有附件的提示照旧走 prompt,带附件的走新方法。后端没有实现新方法时:全是文件链接的提示按“正文、空行、每行一个 [@名称](uri)”交给 prompt;带图片的提示这一回合以 failed 结束。
    • Rust:AgentAttachment {mime_type, path, name, uri}、is_image()、link()、text_with_links,AgentSession::prompt_attachments 默认按上面的规则处理;
    • Go:AgentAttachment、IsImage()、Link()、TextWithLinks,可选接口 AgentAttachmentPrompter.PromptAttachments;
    • Node:可选 promptAttachments(turn, text, attachments),导出 attachmentLink、textWithLinks,index.d.ts 新增 AgentAttachment;
    • Java:新增 AgentAttachment(含静态 textWithLinks),AgentSession.promptAttachments 是默认方法。
  • 测试
    • plugin-agents:image_types_are_distinct_known_types_and_reach_the_catalog(重复值、image/svg+xml、IMAGE/PNG 都被拒绝);catalog_entry_hides_session_functions 确认没声明时目录条目不带 imageTypes。
    • agent-runtime:a_prompt_carries_attachments_as_images_or_file_links。
      • 拒绝:相对路径、不存在的文件、65 个附件、空正文无附件,且拒绝后不开始回合;
      • 分类:声明了的 png 和 jpeg 是图片;没声明的 gif、伪装成 png 的文本、空文件是文件链接;用 set_len 撑到 64 MiB 的 png 仍是图片(不限大小);文件夹是以 / 结尾的文件链接;
      • 文件名里的空格在 uri 里转义成 %20。
    • fixture agents/native-session.json:带一张图片和一个文件链接的 didPrompt;新增 Agent plain,测试后端给它的会话只实现 prompt,用来锁定四种 SDK 的默认处理(文件链接并进正文、空正文只留链接、带图片的回合失败)。四种 SDK 的测试都读这份 fixture。
  • 契约与文档
    • shared/plugins/contract/v1/README.md:native 声明、agents.prompt 参数与拒绝码、didPrompt 的 attachments 和后端的处理方式(图片按 CLI 的格式传,CLI 的模型要更小的图片时缩小而不是让回合失败;文件链接写成 [@名称](uri)),并说明宿主把粘贴的图片放在平台缓存目录;
    • plugin-manifest.schema.json:native.imageTypes;
    • reference/domains/agents.md(中英)、reference/capabilities-and-versions.md(中英)、reference/api-catalog.md(didPrompt 两行)、rust/contracts/rust-core-api.md(agents.prompt 一行);
    • 四种 SDK README 各一段附件说明;
    • reference/protocol-changelog.md:本 PR 的待合入条目;已合入的 feat(agents): Agent 资源库统一管理指令、技能、MCP 服务器和 Hooks,并逐条列出调用(API-45 0 内补充) #223 移到 2026-11-28 时间线。

验证 / Verification

  • cargo test -p lithe-agent-runtime -p lithe-plugin-agents:通过(agent-runtime 28 个,plugin-agents 18 个)。
  • cargo clippy -p lithe-agent-runtime -p lithe-plugin-agents --all-targets -- -D warnings、cargo fmt --all -- --check:通过。
  • 四种 SDK:Rust cargo fmt --check、cargo clippy --all-targets -- -D warnings、cargo test;Go gofmt -l、go vet、go test;Node node --test(94 个)和 tsc 类型测试;Java mvn test(85 个):都通过。
  • node shared/plugins/scripts/sdk-e2e.mjs:通过。
  • node shared/plugins/scripts/verify-docs.mjs:通过。

跟进 / Follow-up

清单 agents[].native 新增可选 imageTypes;agents.prompt 新增可选 images [{path}],
Core 按文件开头字节判断类型并检查数量和总大小,didPrompt 带 images [{mimeType, path}]。
四种官方 SDK 新增图片处理方法,没有实现时这一回合以 failed 结束。
更新记录把已合入的 #223 移到 2026-11-28 时间线。
agents.prompt 的 images 改成 attachments:用户选择、粘贴或拖入的文件、文件夹和图片,
最多 64 个,只传路径、不限大小。Core 按文件开头字节把 Agent 在 native.imageTypes
里声明的类型当作图片,其余(含文件夹、空文件、读不了的文件)当作文件链接;
didPrompt 带 {type, path, name, uri, mimeType?}。错误码改为 agents.invalidAttachment、
agents.tooManyAttachments。四种 SDK 改为 prompt_attachments,没有实现时文件链接以
[@name](uri) 并进正文交给 prompt,带图片的回合失败;fixture 补充 Agent plain。
@xiaoyumuxi xiaoyumuxi changed the title feat(agents): 提示可以带图片,Agent 在清单里声明接受的图片类型(API-45 0 内补充) feat(agents): 提示可以带文件和图片附件,按 ACP 的方式分成图片和文件链接(API-45 0 内补充) Nov 28, 2026
@xiaoyumuxi
xiaoyumuxi marked this pull request as ready for review November 28, 2026 22:02
@xiaoyumuxi
xiaoyumuxi merged commit b881135 into main Nov 28, 2026
11 of 12 checks passed

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 2 potential issues.

3 flags not posted on this PR by your GitHub settings — view them in Devin Review. (Configure)

Devin Review

Comment on lines +1130 to +1132
export function attachmentLink(attachment) {
return `[@${attachment.name}](${attachment.uri})`;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Broken attachment links for bracketed filenames

When a filename contains an unmatched ], attachmentLink inserts it unescaped into the Markdown label. The closing bracket breaks the link, leaving the agent with plain text instead of an attachment link.

Learn more

The SDK turns file attachments into Markdown links for handlers that only accept prompt text. The filename goes into the label unchanged, so an unmatched closing bracket ends the label before the destination. This also affects Go Link, Java link, and Rust link. The parenthesized destination also needs protection from literal parentheses in a file URI.

Example: Attaching draft].md produces [@draft].md](file:///work/draft%5D.md), which a Markdown parser reads as plain text even though the URI is valid.

Recommended fix: Escape Markdown metacharacters in the label and use a destination representation that preserves the URI without letting literal parentheses end the link. Add shared fixture cases for brackets and unbalanced parentheses.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

if !file.is_absolute() {
return Err(invalid());
}
let metadata = std::fs::metadata(file).map_err(|_| invalid())?;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Special files accepted as attachments

When a user attaches a Unix socket or named pipe, attachment accepts it as a file link. The prompt starts a turn instead of returning agents.invalidAttachment.

Suggested change
let metadata = std::fs::metadata(file).map_err(|_| invalid())?;
let metadata = std::fs::metadata(file).map_err(|_| invalid())?;
if !metadata.is_file() && !metadata.is_dir() {
return Err(invalid());
}

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

xiaoyumuxi added a commit that referenced this pull request Nov 29, 2026
“宿主方法(按 API 编号)”表给 5 个 PR 补了 13 行,都是给已有方法或通知加字段、改校验时漏登记的:
- #151:workspace/listContexts、workspace/getContext(authority 新增 platform),functions/invoke(转发远程工作区上下文的调用)
- #184:API-11 的 7 个方法(路由前严格校验参数;workspace/create 的 kind 新增 symlink 与 linkTarget)
- #188:editor/registerProviders(新增 colorDecorations)
- #198:editor/registerActions(actionIcons 新增 4 个导航图标 token)
- #224:agents/didPrompt(有附件时带 attachments)

统一写法:方法列只写方法名,补了什么写在协议列的括号里(#193 的 openHTML、openExternal,#23、#128 的 run/start,#128 的 prepareRun);同一个方法的各行排在一起,首次记录在前。

表格说明、维护第 3 步和 API PR 提交指南第 4 步写明:给已有方法或通知加参数、返回字段或取值,或改执行、校验行为,都在索引另加一行;API 总表不加行,用途、权限或协议变了就改原来那一行。
xiaoyumuxi added a commit that referenced this pull request Nov 29, 2026
* docs(plugins): 协议更新记录把已合入的 PR 移进时间线并回填日期(#222)

“待合入”一节的 20 条记录(含 #224)对应的 PR 都已合入 main,按 UTC
合入日期改成时间线表格行,原文逐句保留,只删去“合入日期在合并时补齐”
之类的占位句;#193 已在时间线里,删去重复的待合入条目。新增 2026-11-24、
2026-11-22 两个日期分组。

- 回填“按协议查首次出现”的市场增量索引 schemaVersion 0(#187)、
  toolchainProviders 1 与 litheToolchains 1(#182),以及“宿主方法
  (按 API 编号)”里 #194、#190、#183、#151 的 18 个方法日期。
- 补录改了契约却没有登记的 #177、#117、#26。
- #207 的修改人按 PR 作者改为 devin-ai-integration[bot]。
- 时间线表头和维护说明统一用“改动”;转义 #133 行内代码里的竖线,
  避免表格多出一列。

* docs(plugins): 已有宿主方法加字段也在方法表补行,统一方法表写法(#222)

“宿主方法(按 API 编号)”表给 5 个 PR 补了 13 行,都是给已有方法或通知加字段、改校验时漏登记的:
- #151:workspace/listContexts、workspace/getContext(authority 新增 platform),functions/invoke(转发远程工作区上下文的调用)
- #184:API-11 的 7 个方法(路由前严格校验参数;workspace/create 的 kind 新增 symlink 与 linkTarget)
- #188:editor/registerProviders(新增 colorDecorations)
- #198:editor/registerActions(actionIcons 新增 4 个导航图标 token)
- #224:agents/didPrompt(有附件时带 attachments)

统一写法:方法列只写方法名,补了什么写在协议列的括号里(#193 的 openHTML、openExternal,#23、#128 的 run/start,#128 的 prepareRun);同一个方法的各行排在一起,首次记录在前。

表格说明、维护第 3 步和 API PR 提交指南第 4 步写明:给已有方法或通知加参数、返回字段或取值,或改执行、校验行为,都在索引另加一行;API 总表不加行,用途、权限或协议变了就改原来那一行。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant